package com.woniu.p2p.behindController;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.read.builder.ExcelReaderSheetBuilder;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
import com.github.pagehelper.PageInfo;
import com.woniu.p2p.behindService.BehindScoreService;
import com.woniu.p2p.entity.UserScore;
import com.woniu.p2p.listener.ScoreListener;
import com.woniu.p2p.vo.ImportScoreVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author 86152
 * @create 2020/8/13
 * @since 1.0.0
 */
@Controller
@RequestMapping("/score")
public class ScoreController {
    @Autowired
    BehindScoreService behindScoreService;
    /*条件查询*/
    @RequestMapping("/getscorebycons")
    public String getscorebycons(Map maps, String status, String keyword, Integer currentPage, @DateTimeFormat(pattern = "yyyy-MM-dd")Date starttime,@DateTimeFormat(pattern = "yyyy-MM-dd")Date endtime){
        currentPage=currentPage==null?1:currentPage;
        int pagesize=4;
        PageInfo<UserScore> userScorePageInfo=behindScoreService.getscorebycons(status,keyword,starttime,endtime,currentPage,pagesize);
        maps.put("scores",userScorePageInfo);
        maps.put("keyword",keyword);
        maps.put("status",status);
        maps.put("starttime",starttime);
        maps.put("endtime",endtime);
        /*查询并返回所有的积分id用于反选*/
        List<UserScore> list=behindScoreService.selectAll();
        ArrayList<Integer> ids = new ArrayList<>();
        for(UserScore score :list){
            ids.add(score.getScoreId());
        }
        maps.put("ids",ids);
        return "content/creditsManager";
    }
    /*根据id删除*/
    @RequestMapping("/deletebyid")
    public String deletebyid(Map maps,String status, String keyword, Integer currentPage, @DateTimeFormat(pattern = "yyyy-MM-dd")Date starttime,@DateTimeFormat(pattern = "yyyy-MM-dd")Date endtime,Integer scoreId){
        currentPage=currentPage==null?1:currentPage;
        int pagesize=4;
        PageInfo<UserScore> userScorePageInfo=behindScoreService.deletebyid(status,keyword,starttime,endtime,currentPage,pagesize,scoreId);
        maps.put("scores",userScorePageInfo);
        maps.put("keyword",keyword);
        maps.put("status",status);
        maps.put("starttime",starttime);
        maps.put("endtime",endtime);
        return "content/creditsManager";
    }
    /*根据id查询单个对象*/
    @RequestMapping("/getscoredetailsbyid")
    @ResponseBody
    public UserScore getscoredetailsbyid(Integer scoreId){
        UserScore userScore=behindScoreService.getscoredetailsbyid(scoreId);
        return userScore;
    }
    /*查询用户id集合*/
    @RequestMapping("/getuseridlist")
    @ResponseBody
    public List<Integer> getuseridlist(){
        List<Integer> list=behindScoreService.getuseridlist();
        return list;
    }
    /*更新用户积分并添加积分变更记录*/
    @RequestMapping("/addscore")
    @ResponseBody
    public Integer addscore(Integer scoreAmount,Integer userId){
        int row=behindScoreService.updatescoreinuserandinsertuserscore(scoreAmount,userId);
        return row;
    }
    /*批量删除*/
    @RequestMapping("/piliangdel")
    @ResponseBody
    public Integer piliangdel(int [] ids){
        int row=behindScoreService.piliangdel(ids);
        return row;
    }

    /*导出*/
    @RequestMapping("/exportscore")
    @ResponseBody
    public void exportcoupon(int[] ids, HttpServletResponse response) throws IOException {
        ServletOutputStream outputStream = response.getOutputStream();
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("测试","UTF-8");
        response.setHeader("Content-Dispostion","attachment;filename*=UTF-8''"+fileName+".xlsx");
        List<UserScore> userScores = new ArrayList<>();
        for(int id:ids){
            UserScore getscoredetailsbyid = behindScoreService.getscoredetailsbyid(id);
            userScores.add(getscoredetailsbyid);
        }
        ExcelWriterBuilder write = EasyExcel.write(outputStream, UserScore.class);
        ExcelWriterSheetBuilder sheet = write.sheet();
        sheet.doWrite(userScores);
    }

    @Autowired
    ScoreListener scoreListener;
    /*导入*/
    @RequestMapping("/importscore")
    @ResponseBody
    public Integer importcoupon(@RequestParam("file") MultipartFile file){
        try {
            ExcelReaderBuilder read = EasyExcel.read(file.getInputStream(), ImportScoreVo.class, scoreListener);
            ExcelReaderSheetBuilder sheet = read.sheet();
            sheet.doRead();
        } catch (IOException e) {
            e.printStackTrace();
            return 0;
        }
        return 1;
    }
}
